package com.myjoje.controller.system;

import com.myjoje.service.system.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文件上传
 */
@Controller
@RequestMapping("/system/file")
public class FileController {

    @Autowired
    private FileService fileService;


    @RequestMapping("/getFileById")
    @ResponseBody
    public Object getFileById(String id) {
        return fileService.getFileById(id);
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        return fileService.upload(multipartFile);
    }

    /**
     * 文件下载
     * /file/download/id
     */
    @RequestMapping("/download/{id}")
    public void download(HttpServletResponse response, @PathVariable String id) throws Exception {
        fileService.download(response, id);
    }

    /**
     * 查看图片
     */
    @RequestMapping("/img/{id}")
    public void img(HttpServletResponse response, @PathVariable String id) throws Exception {
        fileService.download(response, id);
    }

    /**
     * 文件删除（本地和数据库都删除）
     * /file/delete/id
     */
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable String id) throws Exception {
        return fileService.delete(id);
    }

}
